import axios from 'axios'
import qs from 'qs'
import lseutil from './util'
import lsedata from './data'

let baseurl = 'http://127.0.0.1:9092/lse/'
// let baseurl = ''

function getHttpPrefix (index) {
  return 'http://127.0.0.1:9092'
  // return ''
}

// axios 请求
// 当前项目默认不开启session （withCredentials = true）
function post (url, data = {}, qsFormatArray, arrayFormat, isJson) {
  var config = {
    headers: {
      'Content-Type': isJson ? 'application/json;charset=UTF-8' : 'application/x-www-form-urlencoded;charset=UTF-8',
      'TOKEN': storageJson.get(lsedata.tokenName)
    }
  }
  return new Promise((resolve, reject) => {
    axios.post(url, qsFormatArray ? qs.stringify(data, {
      arrayFormat: arrayFormat || 'indices',
      allowDots: true
    }) : isJson ? JSON.stringify(data) : qs.stringify(data), config)
      .then(response => {
        resolve(response.data)
      }, err => {
        lseutil.msg({
          msg: '网络请求错误',
          type: 3
        })
        reject(err)
      })
  })
}

// post 开启session
function postCredentials (url, data = {}, qsFormatArray, arrayFormat) {
  var config = {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
      'TOKEN': storageJson.get(lsedata.tokenName)
    },
    withCredentials: true
  }
  return new Promise((resolve, reject) => {
    axios.post(url, qsFormatArray ? qs.stringify(data, {
      arrayFormat: arrayFormat || 'indices',
      allowDots: true
    }) : qs.stringify(data), config)
      .then(response => {
        resolve(response.data)
      }, err => {
        lseutil.msg({
          msg: '网络请求错误',
          type: 3
        })
        reject(err)
      })
  })
}

// 当前项目默认不开启session （withCredentials = true）
function get (url, data = {}) {
  var config = {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
      'TOKEN': storageJson.get(lsedata.tokenName)
    },
    params: data
  }
  return new Promise((resolve, reject) => {
    axios.get(url, config)
      .then(response => {
        resolve(response.data)
      }, err => {
        lseutil.msg({
          msg: '网络请求错误',
          type: 3
        })
        reject(err)
      })
  })
}

/**
 * 上传文件
 * @param {*} url 文件上传地址
 * @param {*} formData formData 文件封装
 * @param {*} vm 组件对象
 */
function uploadFile (url, formData, vm) {
  var config = {
    headers: {
      'Content-Type': 'multipart/form-data',
      'TOKEN': storageJson.get(lsedata.tokenName)
    },
    // 超时时间 - ms 5min
    timeout: 1000 * 60 * 5,
    onUploadProgress: function (e) {
      // 属性lengthComputable主要表明总共需要完成的工作量和已经完成的工作是否可以被测量
      // 如果lengthComputable为false，就获取不到e.total和e.loaded
      if (e.lengthComputable) {
        // 已上传的比例
        var rate = vm.percentage = parseInt((e.loaded / e.total * 100).toFixed(0))
        console.log('进度' + rate)
        if (rate <= 100) {
          vm.percentage = rate
        }
      }
    }
  }
  return new Promise((resolve, reject) => {
    axios.post(url, formData, config)
      .then(response => {
        resolve(response.data)
      }, err => {
        lseutil.msg({
          msg: '网络请求错误',
          type: 3
        })
        reject(err)
      })
  })
}

// localstorage 操作
const storage = window.localStorage

const storageJson = {
  set: function (key, val) {
    storage.setItem(key, val)
  },
  get: function (key) {
    var tips = storage.getItem(key)
    return tips
  },
  remove: function (key) {
    storage.removeItem(key)
  }
}

export default {baseurl, post, get, storageJson, uploadFile, postCredentials, getHttpPrefix}
